package org.pro.core.config;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * druid数据源管理工具的配置
 * @author luoqiz
 *
 */
@Configuration
public class DruidConfiguration {
	 private static final Logger log = LoggerFactory.getLogger(DruidConfiguration.class);
	 
	 @ConditionalOnClass(DruidDataSource.class)
	 @ConditionalOnProperty(name="spring.datasource.type",havingValue="com.alibaba.druid.pool.DruidDataSource",matchIfMissing=true)
	 static class Druid extends DruidConfiguration{
		 @Bean
		 @ConfigurationProperties("spring.datasource.druid")
		 public DruidDataSource dataSource(DataSourceProperties properties) {
			 DruidDataSource druidDataSource = (DruidDataSource) properties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
			 DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
			 String validationQuery = databaseDriver.getValidationQuery();
			 if(validationQuery!=null) {
				 druidDataSource.setValidationQuery(validationQuery);
			 }
			 return druidDataSource;
		 }
	 }
	 @Bean
	 public ServletRegistrationBean<StatViewServlet> druidServlet() {
		 log.info("init Druid Servlet Configuration "); 
		 ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>();
		 servletRegistrationBean.setServlet(new StatViewServlet()); 
		 servletRegistrationBean.addUrlMappings("/druid/*"); 
		 Map<String, String> initParameters = new HashMap<String, String>(); 
		 initParameters.put("loginUsername", "admin");// 用户名 
		 initParameters.put("loginPassword", "admin");// 密码 
		 initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能 
		 initParameters.put("allow", ""); // IP白名单 (没有配置或者为空，则允许所有访问) 
		 //initParameters.put("deny", "192.168.20.38");// IP黑名单 (存在共同时，deny优先于allow) 
		 servletRegistrationBean.setInitParameters(initParameters); 
		 return servletRegistrationBean;

	 }
	 @Bean
	  public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
		 FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>(); 
		 filterRegistrationBean.setFilter(new WebStatFilter()); 
		 filterRegistrationBean.addUrlPatterns("/*"); 
		 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); 
		 return filterRegistrationBean;
	 }
}
